<template>
  <!-- 显示弹窗-->
  <el-dialog :title="showTitle" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" size="mini" style="width: 80%;" autocomplete="off" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编号" prop="code">
        <el-input v-model="form.code" size="mini" style="width: 80%;" autocomplete="off" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" size="mini" style="width: 80%;" placeholder="请选择负责人">
          <el-option v-for="item in simpleList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" :rows="4" size="mini" style="width: 80%;" autocomplete="off" type="textarea" placeholder="1-100个字符" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit">确 定</el-button>
      <el-button size="mini" @click="closeFn">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartListApi, getDepartSimpleApi, addDepartApi, updateDepartApi, getupdateDepartApi } from '@/api/department'
export default {
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: Number
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        managerId: '',
        name: ''
      },
      simpleList: [],
      rules: {
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getDepartListApi()
              if (this.form.id) {
                res = res.filter(item => item.id !== this.form.id)
              }
              if (res.some(item => item.code === value)) {
                callback(new Error('部门编码已存在'))
              } else { callback() }
            } }
        ],
        introduce: [
          { required: true, message: '请输入部门描述', trigger: 'blur' },
          { min: 1, max: 100, message: '请输入1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getDepartListApi()
              if (this.form.id) {
                res = res.filter(item => item.id !== this.form.id)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else { callback() }
            } }
        ]
      }
    }
  },

  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },

  async created() {
    this.simpleList = await getDepartSimpleApi()
    // console.log(this.simpleList)
  },

  methods: {
    async getDepartInfo(id) {
      const res = await updateDepartApi(id)
      // console.log(res)
      this.form = res
    },
    closeFn() {
      this.form = {
        code: '',
        introduce: '',
        managerId: '',
        name: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async submit() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          await getupdateDepartApi(this.form)
          this.$message.success('修改成功')
        } else {
          await addDepartApi({ ...this.form, pid: this.pid })
          this.$message.success('添加成功')
        }
        this.closeFn()
        this.$emit('updateList')
      } catch (error) {
        console.log(error)
      }
    }
  }
}

</script>
<style  scoped lang="scss">
</style>
